class Solution(object):
    def minDominoRotations(self, tops, bottoms):
        now_set = {tops[0], bottoms[0]}
        n = len(tops)
        for i in range(1, n):
            now_wait = {tops[i], bottoms[i]}
            now_set &= now_wait
        if not now_set:
            return -1
        node = now_set.pop()
        t_count = 0
        b_count = 0
        for i in range(n):
            if tops[i] == node:
                t_count += 1
            if bottoms[i] == node:
                b_count += 1
        return min(t_count, n - t_count, b_count, n - b_count)


data = Solution()
tops = [2, 1, 2, 4, 2, 2]
bottoms = [5, 2, 6, 2, 3, 2]
print(data.minDominoRotations(tops, bottoms))
